package com.beinhospital_sw21_h2_05.beinhospitalsw21h205.service.impl;

import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.entity.Medicine;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.mapper.MedicineMapper;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.service.MedicineService;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.utils.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MedicineServiceImpl implements MedicineService {
    @Autowired
    private MedicineMapper medicineMapper;

    /**
     * 分页查询药品信息
     *
     * @param page         当前页码
     * @param pageSize     每页条数
     * @param medicineName 查询条件，药品名称
     * @return 分页结果
     */
    @Override
    public PageBean page(Integer page, Integer pageSize, String medicineName) {
        // 使用PageHelper进行分页
        PageHelper.startPage(page, pageSize);
        // 查询药品信息列表
        List<Medicine> medicineList = medicineMapper.list(medicineName);
        // 将查询结果包装成PageBean
        Page<Medicine> p = (Page<Medicine>) medicineList;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    /**
     * 删除药品信息
     *
     * @param medicineID 药品ID
     */
    @Override
    public void delete(Integer medicineID) {
        // 调用Mapper进行药品删除操作
        medicineMapper.delete(medicineID);
    }

    /**
     * 添加药品信息
     *
     * @param medicine 药品信息对象
     */
    @Override
    public void add(Medicine medicine) {
        // 调用Mapper进行药品添加操作
        medicineMapper.insert(medicine);
    }

    /**
     * 更新药品信息
     *
     * @param medicine 药品信息对象
     */
    @Override
    public void update(Medicine medicine) {
        // 调用Mapper进行药品更新操作
        medicineMapper.update(medicine);
    }
}
